package Handler;
import Service.IUserService;
import Service.impl.UserServiceImpl;
import dao.*;
import dao.impl.*;
import pojo.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class UserHandler {
    IUserDao userService = new UserDaoImpl();
    IProvinceDao provinceService = new ProvinceDaoImpl();
    ICountryDao countryService = new CountryDaoImpl();
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            IUserService userService=new UserServiceImpl();
            String name = request.getParameter("name");
            System.out.println(name);
            String password = request.getParameter("password");
            System.out.println(password);
            User user  = userService.login(name,password);
            if(user != null){
                user=userService.findByid(user.getId());
                request.getSession().setAttribute("user",user);
                ShopCart shopCart = new ShopCart();
                request.getSession().setAttribute("shopCart",shopCart);
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }
            else {
                ISuperUserDao superUserDao = new SuperUserDaoImpl();
                SuperUser superUser = superUserDao.findByNameAndPassWd(name, password);
                if (superUser != null) {
                    request.getSession().setAttribute("SuperUser", superUser);
                    SuperUserHandler superUserHandler=new SuperUserHandler();
                    superUserHandler.showUser(request,response);
                } else {
                    request.getRequestDispatcher("/user/login.jsp").forward(request, response);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            request.getRequestDispatcher("/user/login.jsp").forward(request,response);
        }
    }

    public void showregister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Country> countryList = countryService.findAll();
        Iterator<Country> countryIterator = countryList.iterator();
        List<Province> provinceList = provinceService.findAll();
        Iterator<Province> provinceIterator = provinceList.iterator();
        req.setAttribute("countryIterator", countryIterator);
        req.setAttribute("provinceIterator", provinceIterator);
        req.getRequestDispatcher("/user/register.jsp").forward(req, resp);
    }

    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        UserInfo userInfo = new UserInfo();
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String country = req.getParameter("country");
        String province = req.getParameter("province");
        System.out.println(name);
        System.out.println(password);
        System.out.println(country);
        System.out.println(province);
        Country country1 = countryService.findById(Integer.parseInt(country));
        System.out.println(country1.getCountryid());
        System.out.println(country1.getCountryName());
        Province province1 = provinceService.findById(Integer.parseInt(province));
        System.out.println(province1.getProvinceid());
        System.out.println(province1.getProvinceName());
        userInfo.setCountry(country1);
        userInfo.setProvince(province1);
        user.setUsername(name);
        user.setPassword(password);
        user.setUserInfo(userInfo);
        userService.addUser(user);
        req.getRequestDispatcher("/user/login.jsp").forward(req, resp);
    }
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Enumeration enumeration = req.getSession().getAttributeNames();
        while (enumeration.hasMoreElements()) {
            req.getSession().removeAttribute(enumeration.nextElement().toString());
        }
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }
    public void toModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int id=Integer.parseInt(request.getParameter("userid"));
            IUserDao userDao=new UserDaoImpl();
            User user=userDao.findByid(id);
            request.setAttribute("upuser",user);
            request.getRequestDispatcher("/user/update.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "去修改用户失败");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }

    }

    public void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
//            IUserDao userDao=new UserDaoImpl();
//            String password = request.getParameter("password");
//            String email = request.getParameter("email");
//            String street1 = request.getParameter("street1");
//            String street2 = request.getParameter("street2");
//            String cellPhone = request.getParameter("cellphone");
//            int id=Integer.parseInt(request.getParameter("userid"));
//            userDao.updateUser(user);
            SuperUser user1= (SuperUser) request.getSession().getAttribute("SuperUser");
            if (user1!=null) {
                request.getRequestDispatcher("/admin/index.do").forward(request, response);
            }else{
                request.getRequestDispatcher("/shop/user/login.jsp").forward(request,response);
            }

        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "修改用户失败");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }

    }
    public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        try {
            IUserService userService=new UserServiceImpl();
            IUserDao userDao=new UserDaoImpl();
            int id=Integer.parseInt(request.getParameter("userid"));
            User user=userService.findByid(id);
            userDao.deleteUser(user);
            request.getRequestDispatcher("/admin/index.do").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "删除失败");
            request.getRequestDispatcher("/shop/error.jsp").forward(request, response);
        }
    }

}
